#include "setup.h"
#include <Adafruit_NeoPixel.h>
#define PIN 13
#define NUM_LEDS 36
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

#include <MegunoLink.h>
#include <Filter.h>
#define MIC_PIN A0 // 音频采集模块接口
ExponentialFilter<long> ADCFilter(1, 0);
int NOISE = 390; // 噪音底线
// define the variables needed for the audio levels
int lvl = 0, minLvl = 0, maxLvl = 10; // tweak the min and max as needed
// 为平滑原始音频信号实例化过滤器类

uint8_t rgb_mode = 0;

uint8_t j = 0;
uint16_t h = 0;
void rgb_init()
{
      pinMode(A0, INPUT);
      strip.begin();
      strip.setBrightness(100);
      strip.show();
}

uint32_t Wheel(byte WheelPos)
{
      WheelPos = 255 - WheelPos;
      if (WheelPos < 85)
      {
            return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
      }
      if (WheelPos < 170)
      {
            WheelPos -= 85;
            return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
      }
      WheelPos -= 170;
      return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void rgb_run()
{

      if (main_cpu_sleep)
      {
            strip.fill(strip.Color(0, 0, 0), 0, strip.numPixels());
            strip.show();
           
      }
      else
      {
            for (uint16_t i = 0; i < strip.numPixels(); i++)
            {
                  strip.setPixelColor(i, Wheel((i + j) & 255));
            }
            strip.show();
            j++;
            delay(10);
            // music_rgb();
      }
}
bool led_v_state = 0;

void music_rgb()
{
      // 读取音频信号并过滤它
      int n, height;
      n = analogRead(MIC_PIN);
      Serial.print(n);
      Serial.print(" ");
      // 移除1.25VDC的MX9614偏置
      n = abs(1023 - n);
      // 硬限制噪音/嗡嗡声
      n = (n <= NOISE) ? 0 : abs(n - NOISE);
      // 应用指数滤波器平滑原始信号
      ADCFilter.Filter(n);
      lvl = ADCFilter.Current();

      // 计算像素的数量作为范围的百分比
      //  to:可以通过使用最小/最大音频级别的运行平均值动态完成
      height = 12 * (lvl - minLvl) / (long)(maxLvl - minLvl);
      if (height < 0L)
            height = 0;
      else if (height > 10)
            height = 10;
      // 打开/关闭电平对应的led灯

      // 绘制原始信号与过滤信号的对比图
      Serial.print(n);
      Serial.print(" ");
      Serial.print(lvl);
      Serial.print(" ");
      Serial.println(height);

      for (uint8_t i = 1; i < 11; i++)
      {
            // 关闭高于当前电平的led灯
            if (i - 1 >= height)
                  strip.setPixelColor(NUM_LEDS - i, strip.Color(0, 0, 0));
            // 否则，打开它们!
            else
            {
                  // int a = map(i + 26, 0, NUM_LEDS - 1, 30, 150); // 30-150
                  // if (a < 85)
                  // {
                  //       Wheel.r = a * 3;
                  //       Wheel.g = 255 - a * 3;
                  //       Wheel.b = 0;
                  // }
                  // else if (a < 170)
                  // {
                  //       a -= 85;
                  //       Wheel.r = 255 - a * 3;
                  //       Wheel.g = 0;
                  //       Wheel.b = a * 3;
                  // }
                  // else
                  // {
                  //       a -= 170;
                  //       Wheel.r = 0;
                  //       Wheel.g = a * 3;
                  //       Wheel.b = 255 - a * 3;
                  // }
                  strip.setPixelColor(NUM_LEDS - i, strip.Color(0x00, 0xff, 0xff));
            }
      }
      strip.show();
}